package com.gitee.hljdrl.ffkit.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import java.util.List;

public abstract class BaseRvAdapter<T, VB extends ViewBinding> extends RecyclerView.Adapter<BaseRvAdapter.ViewHolder<VB>> {

    public final String TAG = this.getClass().getSimpleName();
    protected List<T> mList;

    protected final Context mContext;
    protected OnItemClickListener<T> onItemClickListener;

    protected OnItemLongClickListener<T> onItemLongClickListener;

    protected LayoutInflater mLayoutInflater;

    public BaseRvAdapter(Context mContext, List<T> mList) {
        this.mContext = mContext;
        this.mList = mList;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    protected LayoutInflater getLayoutInflater() {
        return mLayoutInflater;
    }

    /**
     * 更新数据
     */
    public void setList(List<T> list) {
        this.mList = list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return getViewHolder(parent, viewType);
    }

    public void onBindViewHolder(@NonNull ViewHolder<VB> holder, int position) {
        onBindItemViewData(holder.mVB, mList.get(position), position);
    }

    public abstract ViewHolder getViewHolder(@NonNull ViewGroup parent, int viewType);

    public abstract void onBindItemViewData(VB vb, T bean, int position);

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener<T> onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(View v, int position, T t);
    }

    public interface OnItemLongClickListener<T> {
        void onItemLongClickListener(View v, int position, T t);
    }

    public static class ViewHolder<VB extends ViewBinding> extends RecyclerView.ViewHolder {
        private final VB mVB;

        public ViewHolder(@NonNull VB binding) {
            super(binding.getRoot());
            this.mVB = binding;
        }
    }
}
